
import { createRouter, createWebHashHistory } from "vue-router";
import i18n from '~/lang'
import Admin from "~/layouts/admin.vue";
import Index from "~/pages/index.vue";
import Login from "~/pages/login.vue";
import NotFound from "~/pages/404.vue";
// import CategoryList from "~/pages/category/list.vue";
// import OrderList from "~/pages/order/list.vue";
// import CommentList from "~/pages/comment/list.vue";
// import ImageList from "~/pages/image/list.vue";
// import NoticeList from "~/pages/notice/list.vue";
// import SettingBase from "~/pages/setting/base.vue";
// import CouponList from "~/pages/coupon/list.vue";

// import AccessList from "~/pages/access/list.vue";
// import RoleList from "~/pages/role/list.vue";
// import SkusList from "~/pages/skus/list.vue";
// import SettingBuy from "~/pages/setting/buy.vue";
// import SettingShip from "~/pages/setting/ship.vue";
// import DistributionIndex from "~/pages/distribution/index.vue";
// import DistributionSetting from "~/pages/distribution/setting.vue";
// Flowlist:保留
// import FlowList from '~/pages/flow/list.vue'
// import ScreenApp from "~/pages/screen_app.vue"
// UserList:作为流量功能
import UserList from "~/pages/user/list.vue";
// LevelList:作为协议统计
import LevelList from "~/pages/level/list.vue";
// GoodList:作为任务调度
import GoodList from "~/pages/goods/list.vue";
// ManagerList:作为数据导出
// import ManagerList from "~/pages/manager/list.vue"
// 默认路由，所有用户共享
const routes = [
  {
    path: "/",
    name: "admin",
    component: Admin,
    child: [],
  },
  {
    path: "/logIn",
    component: Login,
    meta: {
      title: i18n.global.t("登录页"),
    },
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound,
  },
  {
    path: "/screen_app",
    name: "/screen_app",
    // component: ScreenApp,
    component: () => import("~/pages/screen_app.vue"),
    //resolve=>(require(['需要加载的路由的地址'])，resolve)
    meta: {
      title: i18n.global.t("感知大屏"),
    },
  },
  // {
  //     path: "/screen",
  //     name: "/screen",
  //     component: Screen,
  //     meta: {
  //         title: "感知大屏"
  //     }
  // },
  // {
  //     path: "/flow/list",
  //     name: "/flow/list",
  //     component: FlowList,
  //     meta: {
  //         title: "流量统计"
  //     }
  //     // component: ()=> import("../pages/flow/list100.vue")
  // }
];

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [
  {
    path: "/",
    name: "/",
    component: Index,
    meta: {
      title: i18n.global.t("后台首页"),
    },
  },
  {
    path: "/goods/list",
    name: "/goods/list",
    component: GoodList,
    meta: {
      title: i18n.global.t("任务调度"),
    },
  },

  {
    path: "/user/list",
    name: "/user/list",
    component: UserList,
    meta: {
      title: i18n.global.t("流量功能"),
    },
  },
  {
    path: "/level/list",
    name: "/level/list",
    component: LevelList,
    meta: {
      title: i18n.global.t("协议统计"),
    },
  },
  // {
  //   path: "/order/list",
  //   name: "/order/list",
  //   component: OrderList,
  //   meta: {
  //     title: "订单列表"// 任务管理??
  //   }
  // },
  // {
  //   path: "/category/list",
  //   name: "/category/list",
  //   component: CategoryList,
  //   meta: {
  //     title: "分类列表"
  //   }
  // },
  // {
  //   path: "/comment/list",
  //   name: "/comment/list",
  //   component: CommentList,
  //   meta: {
  //     title: "评价列表"
  //   }
  // },
  // {
  //   path: "/image/list",
  //   name: "/image/list",
  //   component: ImageList,
  //   meta: {
  //     title: "图库列表"
  //   }
  // },
  //
  // {
  //   path: "/setting/base",
  //   name: "/setting/base",
  //   component: SettingBase,
  //   meta: {
  //     title: "配置"
  //   }
  // },
  // {
  //   path: "/access/list",
  //   name: "/access/list",
  //   component: AccessList,
  //   meta: {
  //     title: "菜单权限管理"
  //   }
  // },
  // {
  //   path: "/notice/list",
  //   name: "/notice/list",
  //   component: NoticeList,
  //   meta: {
  //     title: "公告列表"
  //   }
  // },
  // {
  //   path: "/coupon/list",
  //   name: "/coupon/list",
  //   component: CouponList,
  //   meta: {
  //     title: "优惠券列表"
  //   }
  // },
  // {
  //   path: "/manager/list",
  //   name: "/manager/list",
  //   component: ManagerList,
  //   meta: {
  //     title: "数据导出"
  //   }
  // },
  // {
  //   path: "/role/list",
  //   name: "/role/list",
  //   component: RoleList,
  //   meta: {
  //     title: "角色管理"
  //   }
  // },
  // {
  //   path: "/skus/list",
  //   name: "/skus/list",
  //   component: SkusList,
  //   meta: {
  //     title: "规格管理"
  //   }
  // },
  // {
  //   path: "/setting/buy",
  //   name: "/setting/buy",
  //   component: SettingBuy,
  //   meta: {
  //     title: "支付设置"
  //   }
  // },
  // {
  //   path: "/setting/ship",
  //   name: "/setting/ship",
  //   component: SettingShip,
  //   meta: {
  //     title: "物流设置"
  //   }
  // },
  //   {
  //   path: "/distribution/index",
  //   name: "/distribution/index",
  //   component: DistributionIndex,
  //   meta: {
  //     title: "分销员管理"
  //   }
  // },
  // {
  //   path: "/distribution/setting",
  //   name: "/distribution/setting",
  //   component: DistributionSetting,
  //   meta: {
  //     title: "分销设置"
  //   }
  // },
  // {
  //     path: "/flow/list",
  //     name: "/flow/list",
  //     component: FlowList,
  //     meta: {
  //         title: "流量统计"
  //     }
  //     // component: ()=> import("../pages/flow/list100.vue")
  // }
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 动态添加路由的方法
export function addRoutes(menus) {
  // 是否有新的路由
  let hasNewRoutes = false;
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      let item = asyncRoutes.find((o) => o.path === e.frontpath);
      if (item && !router.hasRoute(item.path)) {
        router.addRoute("admin", item);
        hasNewRoutes = true;
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child);
      }
    });
  };

  findAndAddRoutesByMenus(menus);

  return hasNewRoutes;
}
